package com.zd.mock.zhangzhiqiang;

import com.github.javafaker.Faker;
import java.sql.*;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;

public class BusinessAttractionDataGenerator {
    private static final String DB_URL = "jdbc:mysql://hadoop102:3306/zhengzhou_tourism_business?useSSL=false&serverTimezone=UTC";
    private static final String DB_USER = "root";
    private static final String DB_PASSWORD = "000000";
    private static final int BATCH_SIZE = 5;
    private static final Faker faker = new Faker(new Locale("zh-CN"));
    private static final Random random = new Random();

    // 郑州主要景点名称
    private static final String[] ATTRACTION_NAMES = {
            "郑州绿博园", "郑州植物园", "郑州博物馆",
            "郑州科技馆", "郑州二七纪念塔"
    };

    // 郑州区域名称
    private static final String[] REGIONS = {
            "登封市", "惠济区", "金水区", "中牟县",
            "二七区", "中原区", "管城回族区", "郑东新区"
    };

    // 常见开放时间
    private static final String[] OPENING_HOURS = {
            "08:00-17:00", "08:30-18:00", "07:30-17:30",
            "09:00-17:00", "09:30-17:30", "09:00-17:30",
            "08:00-18:00", "08:00-18:30"
    };

    public static void main(String[] args) {
        int count = 6; // 生成50条景区数据
        generateBusinessAttractions(count);
    }

    public static void generateBusinessAttractions(int count) {
        String insertSql = "INSERT INTO business_attractions " +
                "(attraction_id, attraction_name, region_name, address, " +
                "opening_hours, contact_phone, base_score, create_time, update_time) " +
                "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) " +
                "ON DUPLICATE KEY UPDATE update_time = VALUES(update_time)";

        try (Connection conn = DriverManager.getConnection(DB_URL, DB_USER, DB_PASSWORD);
             PreparedStatement pstmt = conn.prepareStatement(insertSql)) {

            conn.setAutoCommit(false);

            for (int i = 1; i <= count; i++) {
                long attractionId = 10000L + i;
                String attractionName = ATTRACTION_NAMES[random.nextInt(ATTRACTION_NAMES.length)];
                String regionName = REGIONS[random.nextInt(REGIONS.length)];
                String address = regionName + attractionName + "景区";
                String openingHours = OPENING_HOURS[random.nextInt(OPENING_HOURS.length)];
                String contactPhone = generatePhoneNumber();
                double baseScore = 4.0 + random.nextDouble() * 1.0; // 4.0-5.0之间的评分

                LocalDateTime createTime = randomDateTimeBetweenDays(365); // 过去一年内
                LocalDateTime updateTime = createTime.plusDays(random.nextInt(30)); // 创建后30天内更新

                pstmt.setLong(1, attractionId);
                pstmt.setString(2, attractionName);
                pstmt.setString(3, regionName);
                pstmt.setString(4, address);
                pstmt.setString(5, openingHours);
                pstmt.setString(6, contactPhone);
                pstmt.setDouble(7, baseScore);
                pstmt.setTimestamp(8, Timestamp.valueOf(createTime));
                pstmt.setTimestamp(9, Timestamp.valueOf(updateTime));

                pstmt.addBatch();
                if (i % BATCH_SIZE == 0) {
                    pstmt.executeBatch();
                }
            }

            pstmt.executeBatch();
            conn.commit();
            System.out.println("成功插入 " + count + " 条 business_attractions 数据");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 生成符合中国规范的景区联系电话
    private static String generatePhoneNumber() {
        String[] prefixes = {"0371-6", "0371-7", "0371-8"};
        String prefix = prefixes[random.nextInt(prefixes.length)];
        String suffix = String.format("%07d", random.nextInt(10000000));
        return prefix + suffix;
    }

    // 生成指定范围内的随机时间
    private static LocalDateTime randomDateTimeBetweenDays(int pastDays) {
        long min = LocalDateTime.now().minusDays(pastDays).atZone(ZoneId.systemDefault()).toEpochSecond();
        long max = LocalDateTime.now().atZone(ZoneId.systemDefault()).toEpochSecond();
        long randomEpoch = min + (long) (Math.random() * (max - min));
        return LocalDateTime.ofEpochSecond(randomEpoch, 0, ZoneId.systemDefault().getRules().getOffset(LocalDateTime.now()));
    }
}